import 'package:erp_widget/src/components/widget/index.dart';
import 'package:flutter/material.dart';

/// github地址：https://github.com/gskinnerTeam/flutter-flextras
class DxScrollRow extends StatelessWidget {
  const DxScrollRow({
    Key? key,
    required this.children,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.textDirection,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.mainAxisSize = MainAxisSize.max,
    this.verticalDirection = VerticalDirection.down,
    this.textBaseline,
    this.scrollViewBuilder,
  }) : super(key: key);

  final List<Widget> children;
  final CrossAxisAlignment crossAxisAlignment;
  final TextDirection? textDirection;
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final VerticalDirection verticalDirection;
  final TextBaseline? textBaseline;
  final Widget Function(Axis direction, Widget child)? scrollViewBuilder;

  @override
  Widget build(BuildContext context) => _DxScrollFlex(
        direction: Axis.horizontal,
        crossAxisAlignment: crossAxisAlignment,
        textDirection: textDirection,
        mainAxisAlignment: mainAxisAlignment,
        mainAxisSize: mainAxisSize,
        verticalDirection: verticalDirection,
        textBaseline: textBaseline,
        scrollViewBuilder: scrollViewBuilder,
        children: children,
      );
}

class DxScrollColumn extends StatelessWidget {
  const DxScrollColumn({
    Key? key,
    required this.children,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.textDirection,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.mainAxisSize = MainAxisSize.max,
    this.verticalDirection = VerticalDirection.down,
    this.textBaseline,
    this.scrollViewBuilder,
  }) : super(key: key);

  final List<Widget> children;
  final CrossAxisAlignment crossAxisAlignment;
  final TextDirection? textDirection;
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final VerticalDirection verticalDirection;
  final TextBaseline? textBaseline;
  final Widget Function(Axis direction, Widget child)? scrollViewBuilder;

  @override
  Widget build(BuildContext context) => _DxScrollFlex(
        direction: Axis.vertical,
        crossAxisAlignment: crossAxisAlignment,
        textDirection: textDirection,
        mainAxisAlignment: mainAxisAlignment,
        mainAxisSize: mainAxisSize,
        verticalDirection: verticalDirection,
        textBaseline: textBaseline,
        scrollViewBuilder: scrollViewBuilder,
        children: children,
      );
}

// Normally having an Expanded in a Column will throw an error if you place that Column in a SingleChildScrollView.
// This widget addresses that issue allowing you to placed Expanded or Flexible content inside a scrollview.
class _DxScrollFlex extends StatelessWidget {
  const _DxScrollFlex({
    Key? key,
    required this.children,
    required this.direction,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.textDirection,
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.mainAxisSize = MainAxisSize.max,
    this.verticalDirection = VerticalDirection.down,
    this.textBaseline,
    this.scrollViewBuilder,
  }) : super(key: key);

  ///
  final List<Widget> children;
  final Axis direction;
  final CrossAxisAlignment crossAxisAlignment;
  final TextDirection? textDirection;
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final VerticalDirection verticalDirection;
  final TextBaseline? textBaseline;
  final Widget Function(Axis direction, Widget child)? scrollViewBuilder;

  @override
  Widget build(BuildContext context) => LayoutBuilder(
        builder: (_, constraints) {
          Widget current = Flex(
            direction: direction,
            crossAxisAlignment: crossAxisAlignment,
            textDirection: textDirection,
            mainAxisAlignment: mainAxisAlignment,
            mainAxisSize: mainAxisSize,
            verticalDirection: verticalDirection,
            textBaseline: textBaseline,
            children: children,
          );
          // Intrinsic width or height stops the flexible from expanding when it's height/width becomes unbounded
          //content = direction == Axis.vertical ? IntrinsicHeight(child: content) : IntrinsicWidth(child: content);
          current = IntrinsicHeight(child: IntrinsicWidth(child: current));
          // Constrained box stops the column/row from getting too small, forcing it to be at least as big as it's parent
          current = ConstrainedBox(
            constraints: BoxConstraints(minWidth: constraints.maxWidth, minHeight: constraints.maxHeight),
            child: current,
          );

          // Use an external scrollBuilder if one is available
          return scrollViewBuilder != null
              ? scrollViewBuilder!.call(direction, current)
              : ScrollConfiguration(
                  behavior: DxNoScrollBehavior(),
                  child: SingleChildScrollView(
                    physics: const BouncingScrollPhysics(),
                    scrollDirection: direction,
                    child: current,
                  ),
                );
        },
      );
}
